package org.edu.modules.research.rproject.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.edu.EduResearchAppliction;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.modules.research.rbase.entity.VeJkyOrganizeUser;
import org.edu.modules.research.rbase.service.VeJkyFileService;
import org.edu.modules.research.rproject.dto.VeJkyGainCountDto;
import org.edu.modules.research.rproject.entity.VeJkyProject;
import org.edu.modules.research.rproject.entity.VeJkyProjectGainUser;
import org.edu.modules.research.rproject.mapper.VeJkyGainMapper;
import org.edu.modules.research.rproject.entity.VeJkyGain;
import org.edu.modules.research.rproject.dto.VeJkyGainDto;
import org.edu.modules.research.rproject.service.VeJkyGainService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.edu.modules.research.rproject.service.VeJkyProjectGainUserService;
import org.edu.modules.research.rproject.service.VeJkyProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 实现类
 */
@Service
public class VeJkyGainServiceImpl extends BaseServiceImpl<VeJkyGain, VeJkyGainMapper> implements VeJkyGainService {
    @Autowired
    private VeJkyProjectService veJkyProjectService;
    @Autowired
    private VeJkyProjectGainUserService projectGainUserService;

    @Autowired
    private VeJkyFileService veJkyFileService;

    public IPage<VeJkyGainDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }

    @Override
    public IPage<VeJkyGainCountDto> countUser(BasePageDto page, Wrapper queryWrapper) {
        return mapper.countUser(page,queryWrapper);
    }

    @Override
    public IPage<VeJkyGainCountDto> countCate(BasePageDto page, Wrapper queryWrapper) {
        return mapper.countCate(page,queryWrapper);
    }

    /**
    * 查询实体类
    */
    public VeJkyGainDto findDtoById(String id) {
        VeJkyGain entity = super.findById(id);
        VeJkyGainDto dto = ConvertUtils.convertToDto(entity, VeJkyGainDto.class);


        LambdaQueryWrapper<VeJkyProjectGainUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(VeJkyProjectGainUser::getJkyUserId);
        lambdaQueryWrapper.eq(VeJkyProjectGainUser::getGainId,dto.getId());
        List<VeJkyProjectGainUser> veJkyOrganizeUsers = projectGainUserService.listByWrapper(lambdaQueryWrapper);
        List<Integer> userIds = new ArrayList<>();
        for (VeJkyProjectGainUser user: veJkyOrganizeUsers) {
            userIds.add(user.getJkyUserId());
        }
        dto.setUserIds(userIds);

        return dto;
    }

    /**
    * 新增或者修改
    */
    public void insertOrUpdate(VeJkyGainDto dto,Boolean isAdd){
        Date now  =new Date();
        if(dto.getProjectId()!=null && dto.getProjectId()>0){
            VeJkyProject project = veJkyProjectService.findById(dto.getProjectId() + "");
            dto.setLevelId(project.getLevelId());
            dto.setProjectName(project.getName());
            dto.setCateId(project.getCateId());
        }
        if(isAdd){
            dto.setCreateTime(now);
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            dto.setAddUid(   sysUser.getId());
            super.insert(dto);
        }else{
            projectGainUserService.deleteByPId(dto.getId());
            super.update(dto);
        }

        List<VeJkyProjectGainUser> users = new ArrayList<>();
        for (Integer userId:dto.getUserIds()) {
            VeJkyProjectGainUser user = new VeJkyProjectGainUser();
            user.setJkyUserId(userId);
            user.setGainId(dto.getId());
            user.setCreateTime(now);
            users.add(user);
        }
        projectGainUserService.insert(users);


        veJkyFileService.updateFiles(dto.getFileIds(),dto.getId()+"","gain");
    }

}
